﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Data;
using WPFExcelReport.Helpers;
using System.Linq;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {
        public void YGRVVVK()
        {
            string msg = "";
            string sdkey = "";
            string src = "";
            try
            {  
                DataRow dwVV = dsSource.Tables["VVLIEF"].Rows[0];
                DataRow dwVKK = dsSource.Tables["VKKOPF"].Rows[0];
                DataRow dwVKP = dsSource.Tables["VKPOS"].Rows[0];
                string VKPMEEH = ""; 
                string order = ""; 
                VKPMEEH = dwVKP["VKPMEEH"].ToString();
                #region=============Header==================
                exAgent.SetCellValue(4, 32, date);
                order = Helper.GetSubRollNum(dwVV["VVLIEFSCH"].ToString());
                exAgent.SetCellValue(11, 8, order, true);
                exAgent.RenameSheet(order);
                #region=====sales======
        
                var qpr = from DataRow row in dsSource.Tables["HRSTAMM"].AsEnumerable()
                          where row["HRBENID"].ToString().Trim() == dwVKK["VKKSACHBE"].ToString().Trim()
                          select row;
                foreach (DataRow dwHR in qpr)
                { 
                    exAgent.SetCellValue(8, 28, GetGB2312Value(dwHR["HRNNAME"].ToString().Substring(0, 6)));
                    exAgent.SetCellValue(9, 28, dwHR["HRPTEL"].ToString().Trim());
                    exAgent.SetCellValue(10, 28, dwHR["HRDFAX"].ToString().Trim());
                    break;
                }
                #endregion
                #region=====Buyer======
 
                var qkd = from dwTemp in dsSource.Tables["KDSTAMM"].AsEnumerable()
                          where dwTemp["KDKUNDNR"].ToString() == dwVKK["VKKKUNDNR"].ToString()
                          select dwTemp;
                foreach (DataRow dwKD03 in qkd)
                {
                 
                    //   src = "客户编号/Client No:";
                    //exAgent.SetCellValue(8, 30, src); 
                    string kdnr = dwKD03["KDKUNDNR"].ToString();
                    //src = dwKD03["KDKNAME"].ToString();
                    //exAgent.SetCellValue(4, 7, Helper.GetGB2312Value(src).Trim());
                    string kdname = dwKD03["KDNAME1"].ToString();
                    exAgent.SetCellValue(6, 5, Helper.GetGB2312Value(kdname).Trim() );
                    exAgent.SetCellValue(3, 32,kdnr);
                    exAgent.SetCellValue(7, 5, Helper.GetGB2312Value(dwKD03["KDSTRASSE1"].ToString()).Trim());
                    exAgent.SetCellValue(8, 5, Helper.GetGB2312Value(dwVKK["VKKANSPRECH"].ToString()).Trim());
                    exAgent.SetCellValue(9, 5, dwKD03["KDTEL"].ToString().Trim());
                    exAgent.SetCellValue(10, 5, dwKD03["KDFAX"].ToString().Trim());
                    break;
                    /* src = dwKD03["KDMAILADR"].ToString();
                   exAgent.SetCellValue(10, 1, Helper.GetGB2312Value(src).Trim()); */

                }
                #endregion
               
               exAgent.SetCellValue(12, 8, dwVV["VVFRDAT"].ToString());
               exAgent.SetCellValue(11, 21, dwVKK["VKKAUFKRS"].ToString()+Helper.GetSubRollNum(dwVKK["VKKAUFLFD"].ToString()));
               exAgent.SetCellValue(12, 21, dwVKK["VKKNEUDAT"].ToString());
                exAgent.SetCellValue(11, 31, dwVKK["VKKBESTNR"].ToString());
                exAgent.SetCellValue(12, 31, dwVKK["VKKBESTDAT"].ToString());
                #endregion
                src = dwVKK["VKKZAHLBED"].ToString();//付款方式
                var qz = from dwTemp in dsSource.Tables["ICC_TXTKON"].AsEnumerable()
                         where dwTemp["TXTIPIND"].ToString().Trim() == src.Trim() + "V"
                         select dwTemp;
                foreach (DataRow row in qz)
                {
                    exAgent.SetCellValue(130, 5, row["TXT010"].ToString());
                }
                src = dwVKK["VKKVERSART"].ToString();//运输方式Truck  
                exAgent.SetCellValue(131, 5, Helper.GetSDDAT(dsSource, "0208", src, "V").Trim()); 
                //交易类型FOB
                src = dwVKK["VKKLIEFBED"].ToString();//交易类型FOB 
                Helper.GetSDDAT(dsSource,"0241", src,"V").Trim(); 
                int xStart = 19;
                int rown = 2;
                int idx = 0;
                string finish = "";
                int box=0;
                double tgross = 0, tnet = 0;
                DataTable tbVVT = dsSource.Tables["VVTLIEF"]; 
                var qVVT = from XD in tbVVT.AsEnumerable()
                           where XD["VVTLIEFSCH"].ToString() == dwVV["VVLIEFSCH"].ToString()
                            group XD by new
                            {
                                LIEFSCH = XD["VVTLIEFSCH"].ToString(),
                                VVTAUFKRS = XD["VVTAUFKRS"].ToString(),
                                VVTAUFLFD = XD["VVTAUFLFD"].ToString(),
                                VVTAUFPOS = XD["VVTAUFPOS"].ToString(),
                            }
                                into g
                                select new
                                {
                                    KEY = g.Key,
                                    VVTKG = g.Sum(XD =>Convert.ToDecimal(XD["VVTKG"].ToString())),
                                    VVTNETKG =  g.Sum(XD =>Convert.ToDecimal(XD["VVTNETKG"].ToString())),
                                };
                int count = qVVT.Count();
                string KRS, LFD, POS, LIEFSCH;
                foreach (var dwPOS in qVVT)
                {
                    //xStart=last row
                    KRS = (string)dwPOS.KEY.VVTAUFKRS;
                    LFD = (string)dwPOS.KEY.VVTAUFLFD;
                    POS = (string)dwPOS.KEY.VVTAUFPOS;
                    LIEFSCH = (string)dwPOS.KEY.LIEFSCH;

                    exAgent.SetCellValue(xStart, 1, idx);
                    double gross = 0;
                    double net = 0;
                    gross = Convert.ToDouble((Decimal)dwPOS.VVTKG) / 1000;
                    tgross += gross;
                    net = Convert.ToDouble((Decimal)dwPOS.VVTNETKG) / 1000;
                    tnet += net;
                    exAgent.SetCellValue(xStart, 11, gross);
                    exAgent.SetCellValue(xStart, 15, net);
                    
                    var qVKP = from DataRow row in dsSource.Tables["VKPOS"].AsEnumerable()
                               where row["VKPAUFKRS"].ToString() == KRS
                               && row["VKPAUFLFD"].ToString() == LFD
                               && row["VKPAUFPOS"].ToString() ==POS
                               select row;
                    foreach (DataRow vkp in qVKP)
                    {
                        exAgent.SetCellValue(xStart, 2, vkp["VKPART"].ToString().Trim() + Environment.NewLine +GetGB2312Value(vkp["VKPKURZBEZ"].ToString().Trim()));
                        exAgent.SetCellValue(xStart, 6, vkp["VKPFARBE"].ToString().Trim() + Environment.NewLine +GetGB2312Value(vkp["VKPKDFARBNR"].ToString().Trim()));

                        #region================成份===================                      
                        var qVA07 = from DataRow row in dsSource.Tables["VAART"].AsEnumerable()
                                    where row["VAAART"].ToString() == vkp["VKPART"].ToString()
                                    select row;
                        foreach (DataRow row in qVA07)
                        {
                            string cv = Helpers.Helper.GetCompositionVA(dsSource, row, "V");
                            string ce = Helpers.Helper.GetCompositionVA(dsSource, row, "E");
                            exAgent.SetCellValue(xStart, 19, GetGB2312Value(cv+Environment.NewLine+ce));
                        }
                        #endregion
                        idx++;
                        xStart = xStart + rown * idx;//完成行初始化    
                        #region================清单===================
                        var qPack = from DataRow row in dsSource.Tables["GPACK"].AsEnumerable()
                                    where row["GPAAUFKRS"].ToString() ==KRS
                                    && row["GPAAUFLFD"].ToString() == LFD
                                    && row["GPAAUFPOS"].ToString() ==POS
                                    && row["GPALIEFSCH"].ToString() == LIEFSCH
                                    select row;
                       int pcount = qPack.Count();
                        int pidx = 0;
                        
                        foreach (DataRow row in qPack)
                        {
                            exAgent.SetCellValue(xStart, 2,Helper.GetSubRollNum( row["GPAPACKNR"].ToString()));
                            exAgent.SetCellValue(xStart, 5, Helper.GetSubRollNum(row["GPALBEHI"].ToString()));
                            gross = Convert.ToDouble(row["GPAKG"].ToString()) / 1000;
                            net = Convert.ToDouble(row["GPAKGNET"].ToString()) / 1000;
                            exAgent.SetCellValue(xStart, 11, gross);
                            exAgent.SetCellValue(xStart, 15, net);
                            exAgent.SetCellValue(xStart, 27,Helper.GetSubRollNum( row["GPASPPART"].ToString()));
                            exAgent.SetCellValue(xStart, 32, row["GPAFBPART"].ToString());
                            pidx++;
                            xStart++;
                            if (pidx < pcount)
                            {
                                exAgent.RangeCopy(21, 21, xStart, xStart);
                            }
                        }
                        #endregion
                     
                        xStart++;
                        if (idx < count)
                        {
                            exAgent.RangeCopy(19, 21, xStart, xStart+3);
                        }
                    }      
                }
                exAgent.SetCellValue(117, 11, tgross, true);
                exAgent.SetCellValue(117, 19, tnet, true);       
            }
            catch (Exception ex)
            {
                msg = "有错误产生，请注意:";
                EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            { 
                EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
            }
        }
    
    }
}
